package org.bit.demo;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/response")
//@RestController
@Controller
public class ResponseController {

    @RequestMapping("/returnPage")
    public String returnPage() {
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData() {
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml() {
        return "<h1>一级标题</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public List<Person> returnJson() {
        List<Person> people = new ArrayList<>();
        Person person = new Person("zhangsan", "123456", 25);
        people.add(person);
        return people;
    }

    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String, String> returnMap() {
        Map<String,String> map = new HashMap<>();
        map.put("k1", "v1");
        map.put("k2", "v2");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(404);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response) {
        response.setHeader("myHeader1", "myHeader2");
        return "设置 Header 成功";
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces = "application/json")
    public String setContentType(HttpServletResponse response) {
        return "{\"OK\": 111}";
    }
}
